% avg task time

clear all
close all

GAME_TIME = 600;

%root = "/home/kamil/magisterka/workspace/mgr_tests/Debug";
root="/media/d/mgr_wyniki";

%resultsDir = "00_test_results";
%resultsDir = "01_y_ABC_b_ABC.sh_results"
%resultsDir="02_y_none_b_ABC.sh_results"
%resultsDir="03_y_A_b_ABC.sh_results"
%resultsDir="04_y_B_b_ABC.sh_results"
%resultsDir="05_y_C_b_ABC.sh_results"
resultsDir="06_y_AB_b_ABC.sh_results"
%resultsDir="07_y_AC_b_ABC.sh_results"
%resultsDir="08_y_BC_b_ABC.sh_results"
%resultsDir="11_y_none_b_none.sh_results"

dirName = [root "/" resultsDir];

teamBlue = ["blue0"; "blue1"; "blue2"; "blue3"; "blue4"];
teamYellow = ["yellow0"; "yellow1"; "yellow2"; "yellow3"; "yellow4"];

%teamBlue = ["blue0"];
%teamYellow = ["yellow0"];

files = readdir(dirName);

statsDirs = [];

for i = 1 : rows(files)
      index = strfind(files{i}, "_stats");
      if any(index) == 1
      	statsDirs = [statsDirs; files{i}];
      endif
endfor

printf("Found %d dirs with the statistics\n",  rows(statsDirs));

%	static const int STOP 		= 1000;
%	static const int SHOOT 		= 1001;
%	static const int PROTECT_GOAL	= 1002;
%	static const int CAPTURE_BALL	= 1003;
%	static const int PASS_THE_BALL	= 1004;
%	static const int WAIT_FOR_PASS	= 1005;
%	static const int HOLD_POSITION 	= 1006;
%	static const int COVER		= 1007;

baseStruct		   =  struct("STOP",[],
	                                       "SHOOT",[],
	                                       "PROTECT_GOAL",[],
	                                       "CAPTURE_BALL",[],
	                                       "PASS_THE_BALL",[],
	                                       "WAIT_FOR_PASS",[],
	                                       "HOLD_POSITION",[],
	                                       "COVER",[]);
	                                       
yellowTeamTaskTimesTotal = baseStruct;
blueTeamTaskTimesTotal = baseStruct;


for i = 1 : rows(statsDirs)
     printf(".");
      statsDir = [dirName, "/", statsDirs(i,:)];
      %getting paths to team members statistic files
      yellowStatFiles = strcat(statsDir, "/", teamYellow, ".stats");
      blueStatFiles = strcat(statsDir, "/", teamBlue, ".stats");
      
      yellowTeamTaskTimesInExp = baseStruct;
      blueTeamTaskTimesInExp = baseStruct;
      
      %========================
   %   printf("Yellow ============\n")
      for j = 1 : rows(yellowStatFiles)
	yellowTeamTaskTimesInFile = countAvgTaskTimes(yellowStatFiles(j,:));
	for [val, key] = yellowTeamTaskTimesInFile
		yellowTeamTaskTimesInExp.(key) = [yellowTeamTaskTimesInExp.(key),val];
	endfor
     endfor
     
     yellowTeamTaskTimesInExp;
     
     for [val, key] = yellowTeamTaskTimesInExp
          yellowTeamTaskTimesInExp.(key) = mean(yellowTeamTaskTimesInExp.(key));
     endfor     
      %========================             
        %   printf("Blue ============\n")
      for j = 1 : rows(blueStatFiles)
	blueTeamTaskTimesInFile = countAvgTaskTimes(blueStatFiles(j,:));
	for [val, key] = blueTeamTaskTimesInFile
		      blueTeamTaskTimesInExp.(key) = [blueTeamTaskTimesInExp.(key),val];		
	endfor
     endfor
     
     blueTeamTaskTimesInExp;

     for [val, key] = blueTeamTaskTimesInExp
          blueTeamTaskTimesInExp.(key) = mean(blueTeamTaskTimesInExp.(key));
     endfor     
     %============= adding to total =============
     
     for [val,key] = yellowTeamTaskTimesTotal
     	yellowTeamTaskTimesTotal.(key) = [val, yellowTeamTaskTimesInExp.(key)];
     endfor
     
     for [val,key] = blueTeamTaskTimesTotal
     	blueTeamTaskTimesTotal.(key) = [val, blueTeamTaskTimesInExp.(key)];
     endfor
     
endfor

%== calculating mean time for the total results =====

yellowTeamTaskTimesTotal;
blueTeamTaskTimesTotal;

for [val,key] = yellowTeamTaskTimesTotal
%     	yellowTeamTaskTimesTotal.(key) = mean(val) * GAME_TIME;
     	yellowTeamTaskTimesTotal.(key) = mean(val(find(val>0))) * GAME_TIME;
endfor
     
for [val,key] = blueTeamTaskTimesTotal
%     	blueTeamTaskTimesTotal.(key) = mean(val) * GAME_TIME;
     	blueTeamTaskTimesTotal.(key) = mean(val(find(val>0))) * GAME_TIME;
endfor

printf("\n");

yellowTeamTaskTimesTotal
blueTeamTaskTimesTotal